import { apiLink } from "../config/url"

class MyUpload {

    constructor() {
        this.uploadLink = apiLink + '/upload';
    }

    init() {
        return new Promise((resolve, reject) => {
            wx.chooseImage({
                count: 1,
                sourceType: ['album', 'camera'],
                sizeType: ['original', 'compressed'],
                success: (res) => {
                    const filePath = res.tempFilePaths[0];
                    wx.uploadFile({
                        url: this.uploadLink,
                        filePath,
                        name: 'file',
                        formData: {
                            'folder': 'wxapp_maka',
                        },
                        success: (res) => {
                            const data = JSON.parse(res.data);
                            if (data && data.code == 0) {
                                resolve(data.result);
                            } else {
                                reject('上传失败')
                            }
                        }
                    })
                },
                fail() {
                    reject('上传失败')
                }
            })
        })
    }
}

export default MyUpload;